package com.kzd.btgame.main.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.kzd.btgame.custom.createViewTextBottom
import com.kzd.btgame.custom.setTransitionStatusE
import com.kzd.btgame.databinding.IncludeAppRefreshBinding
import com.kzd.btgame.jetpack.vm.HomeViewModel
import com.kzd.btgame.main.home.adapter.*
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class HomeTravelFragment : OwnerViewBindingFragment<IncludeAppRefreshBinding>() {

    companion object {
        fun newInstance(): HomeTravelFragment {
            return HomeTravelFragment()
        }
    }

    private val mHomeTravelAdapter = HomeTravelAdapter()

    private val mFooterViewBottom: View by lazy {
        createViewTextBottom(context = requireActivity(),"更多游戏请到游戏大厅中查看")
    }

    private val mViewModel: HomeViewModel by lazy {
        ownerViewModel(requireParentFragment())
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> IncludeAppRefreshBinding
        get() = IncludeAppRefreshBinding::inflate

    override fun initView() {

        mViewBinding.rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
            itemAnimator = null
        }.adapter = mHomeTravelAdapter

        mHomeTravelAdapter.setOnItemClickListener { _, _, position ->
//            val itemData = mHomeTravelAdapter.getItem(position)
//            if (itemData.type == HOME_TRAVEL_TYPE_0) {
//                return@setOnItemClickListener
//            }
//            val index = when (itemData.type) {
//                HOME_TRAVEL_TYPE_1 -> 0
//                HOME_TRAVEL_TYPE_2 -> 2
//                else -> 0
//            }
//            toGameTravelActivity(index)

            LogUtils.e(GsonUtils.toJson(mViewModel.reserveChangeLiveData.value?.data))
        }

        mViewBinding.mSmartRefreshLayout.apply {
            setEnableLoadMore(false)
        }.setOnRefreshListener {
            mViewModel.travel()
        }
    }

    override fun initData() {
        mViewModel.travel()
    }

    override fun initViewMode() {
        mViewModel.travelLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response,
                mHomeTravelAdapter,
                mViewBinding.mSmartRefreshLayout,
                (false))
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mHomeTravelAdapter.recyclerView
                .setItemViewCacheSize(data.size)
            mHomeTravelAdapter.setNewInstance(data)
            mHomeTravelAdapter.setFooterView(mFooterViewBottom)
        })

        mViewModel.reserveChangeLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val position = response.data?.position
                ?: return@observe
            val data = response.data?.data
                ?: return@observe
            mHomeTravelAdapter.setGame6AdapterData(position, data)
        })
    }

    override fun onResume() {
        super.onResume()
        mHomeTravelAdapter.startBannerViewSwipe()
    }

    override fun onPause() {
        super.onPause()
        mHomeTravelAdapter.stopBannerViewSwipe()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mHomeTravelAdapter.destroyBannerViewSwipe()
    }
}